
import {
  getListBySmiAndImiApi,
  addRepImiApi,
  modifyRepImiApi,
  deleteRepImiApi
} from '@/services/packetPush.js';
import { message } from 'ant-design-vue';
export default {
  namespaced: true,
  state: {
    listBySmiAndImiLoading: false,
    listBySmiAndImiList: [],
    addRepImiLoading: false,
    paginationOptions: {
      pageNum: 1,
      pageSize: 10,
      total: 0
    }
  },
  mutations: {
    setState(state, { key, payload }) {
      state[key] = payload;
    }
  },
  actions: {
    // 获取列表
    async  getListBySmiAndImi({ commit, state }, params) {
      commit('setState', { key: 'listBySmiAndImiLoading', payload: true });
      const { pageNum, pageSize } = state.paginationOptions;
      const apiParams = {
        pageNum,
        pageSize,
        ...params
      };
      const rep = await getListBySmiAndImiApi(apiParams);
      commit('setState', { key: 'listBySmiAndImiList', payload: rep.data.list });
      commit('setState', { key: 'paginationOptions', payload: { ...state.paginationOptions, total: rep.data.total }});
      commit('setState', { key: 'listBySmiAndImiLoading', payload: false });
    },
    // async getListBySmiAndImi({ commit }, params) {
    //   commit('setState', { key: 'listBySmiAndImiLoading', payload: true });
    //   const rep = await getListBySmiAndImiApi(params);
    //   commit('setState', { key: 'listBySmiAndImiList', payload: rep.data });
    //   commit('setState', { key: 'listBySmiAndImiLoading', payload: false });
    // },
    // 新增
    async addRepImi({ commit }, params) {
      commit('setState', { key: 'addRepImiLoading', payload: true });
      try {
        const rep = await addRepImiApi(params);
        if (rep.data === '成功') {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addRepImiLoading', payload: false });
      }
    },
    // 删除
    async deleteRepImi({ commit }, params) {
      try {
        const rep = await deleteRepImiApi(params);
        if (rep.data === '成功') {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      }
    },
    // 修改
    async modifyRepImi({ commit }, params) {
      commit('setState', { key: 'addRepImiLoading', payload: true });
      try {
        const rep = await modifyRepImiApi(params);
        if (rep.data === '成功') {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addRepImiLoading', payload: false });
      }
    }

  }
};
